Page({
  data: {
    dialogVisible: false,
    list: [],
    isFirst: true,
  },
  onShow() {
    this.getHouseList()
  },

  async swipeClose(ev) {
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      const res = await wx.showModal({
        title: '是否删除已绑定房屋？',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#848484',
        confirmText: '删除',
        confirmColor: '#5591af',
      })
      if (res.confirm) {
        await wx.http.delete(`/room/${ev.mark.id}`)
        this.setData({
          list: this.data.list.filter((item) => item.id !== ev.mark.id),
        })
      }
      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  goDetail(e) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + e.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },

  async getHouseList() {
    const res = await wx.http.get('/room')
    this.setData({ list: res.data, isFirst: false })
  },
})
